//
//  DCHBas2DAniGrp.m
//  GueiTuMerchant_iOS
//
//  Created by 丁诚昊 on 15/8/28.
//  Copyright © 2015年 丁诚昊. All rights reserved.
//

#import "DCHBas2DAniGrp.h"
#import <ImageIO/ImageIO.h>

#pragma mark - Helper * Core Animation 核心动画与工厂方法

@implementation DCHBas2DAniGrp

/** 抖动动画 */
static int numberOfShakes = 5;//震动次数
+ (CAKeyframeAnimation *)shakeAnimation:(CGRect)frame durationTime:(CGFloat)duration;
{
    CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef shakePath = CGPathCreateMutable();
    CGPathMoveToPoint(shakePath, NULL, CGRectGetMidX(frame), CGRectGetMidY(frame) );
    for (int index = 0; index < numberOfShakes; ++index)
    {
        CGPathAddLineToPoint(shakePath, NULL, CGRectGetMidX(frame) - arc4random_uniform(numberOfShakes*1.5),CGRectGetMidY(frame));
        
        CGPathAddLineToPoint(shakePath, NULL,  CGRectGetMidX(frame) + arc4random_uniform(numberOfShakes*1.5),CGRectGetMidY(frame));
    }
    
    CGPathCloseSubpath(shakePath);
    shakeAnimation.path = shakePath;
    shakeAnimation.duration = duration;
    CFRelease(shakePath);
    
    return shakeAnimation;
}

static float startFValue = 1.33f;
+(void) DCHRotate:(UIView *)view withDuration:(CGFloat)duration
{
    float fuckingValue = .1f;
    view.alpha = 0.55f;

    if(duration <= 3.2f){
        duration += fuckingValue;
    }else{
        duration -= fuckingValue;
    }
    
    CATransform3D aniRotate = CATransform3DMakeRotation(M_PI, 1, 0, 0);
    
    [UIView animateWithDuration:duration animations:^{
        if(view.subviews.count > 1){
            view.layer.transform  = aniRotate;
        }
        view.alpha = 1.0f;
    } completion:^(BOOL finished) {
        startFValue -= fuckingValue;
        [UIView animateWithDuration:duration animations:^{
            view.transform = CGAffineTransformIdentity;
        }];
    }];
}

/////////////////////////////////////////////////////////
// ----------------- 3D 动画 ------------------- // 暂未启用
/////////////////////////////////////////////////////////
CATransform3D CATransform3DMakePerspective(CGPoint center, float disZ)
{
    CATransform3D transToCenter = CATransform3DMakeTranslation(-center.x, -center.y, 0);
    CATransform3D transBack = CATransform3DMakeTranslation(center.x, center.y, 0);
    CATransform3D scale = CATransform3DIdentity;
    scale.m34 = -1.0f/disZ;
    return CATransform3DConcat(CATransform3DConcat(transToCenter, scale), transBack);
}

CATransform3D CATransform3DPerspect(CATransform3D t, CGPoint center, float disZ)
{
    return CATransform3DConcat(t, CATransform3DMakePerspective(center, disZ));
}

/////////////////////////////////////////////////////////
// ----------------- 3D 动画 ------------------- // 暂未启用
/////////////////////////////////////////////////////////

/**gif 转换 array 数组动画底层算法 */
+ (NSArray *)ZYPraseGIFDataToImageArray:(NSString *)fileName;
{
    
    NSData *data=[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:fileName ofType:@"gif"]];
    NSMutableArray *frames = [[NSMutableArray alloc] init];
    CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)data, NULL);
    CGFloat animationTime = 0.f;
    if (src) {
        size_t l = CGImageSourceGetCount(src);
        frames = [NSMutableArray arrayWithCapacity:l];
        for (size_t i = 0; i < l; i++) {
            CGImageRef img = CGImageSourceCreateImageAtIndex(src, i, NULL);
            NSDictionary *properties = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(src, i, NULL));
            NSDictionary *frameProperties = [properties objectForKey:(NSString *)kCGImagePropertyGIFDictionary];
            NSNumber *delayTime = [frameProperties objectForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
            animationTime += [delayTime floatValue];
            if (img) {
                [frames addObject:[UIImage imageWithCGImage:img]];
                CGImageRelease(img);
            }
        }
    }
    return [frames copy];
}

/** 点阵3次放大方法 */
+(void) DCHTinySlide:(UIView*)view{
    CABasicAnimation *basicAnimation = [[CABasicAnimation alloc] init];
    basicAnimation.keyPath = @"transform.scale";
    basicAnimation.toValue = @.25f;
    basicAnimation.repeatCount = 3;
    
    [view.layer addAnimation:basicAnimation forKey:nil];
}

//苹果官方的水滴算法 或增加一个block动画操作快到WFView绑定块属性
+ (void)DCHCubeTransition:(UIView*)animatingView{
    CATransition* animation;

    //1.动画 ：------------块旋------------
    animation = [CATransition animation];
    animation.duration = 1.50f;
    animation.subtype = @"fromBottom";
    animation.type = @"rippleEffect";//全局动画
    [animatingView.layer addAnimation:animation forKey:nil];
}
@end
